package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.databinding.BusNearbyLineItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusLineNearbyRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryStandLinesRes;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/15 11:26
 * description:
 */
public class BusSiteDetailAdapter extends BaseRecyclerAdapter<BusSiteDetailAdapter.BusNearbyLineHolder> {
    private FragmentActivity mContext;
    private List<QueryStandLinesRes.BusLineArraysBean> viewData;
    private List<QueryBusLineNearbyRes.BusLineArrayListBean.BusLineListBean> mList = new ArrayList<>();

    public BusSiteDetailAdapter(FragmentActivity activity) {
        mContext = activity;

    }

//    @Override
//    public int getItemViewType(int position) {
//        return R.layout.bus_nearby_line_item;
//    }

    @Override
    public BusNearbyLineHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return BusNearbyLineHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(BusNearbyLineHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(final int position, BusNearbyLineItemBinding binding) {
        if (viewData != null) {

            binding.busHomeLineName.setText(viewData.get(position).getLine_name());
            RecyclerView nearbyBusRv = binding.nearbyBusRv;
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            BusLineAdapter busLineAdapter = new BusLineAdapter(mContext);
            busLineAdapter.setBaseInterface(new BusLineAdapter.BaseInterface() {
                @Override
                public void onClick(View view, int posi) {
                    QueryStandLinesRes.BusLineArraysBean busLineArraysBean = viewData.get(position);
                    QueryStandLinesRes.BusLineArraysBean.BusLineListBean busLineListBean = busLineArraysBean.getBusLineList().get(posi);
                    String line_name = busLineListBean.getLine_name();
                    Bundle bundle = new Bundle();
                    bundle.putString("line_guid", busLineListBean.getLine_guid());
                    bundle.putString("line_name", line_name);
                    JumpUtils.jumptoNatvie(mContext, "RealTimeLineActivity", 0, bundle);
                }
            });

            nearbyBusRv.setLayoutManager(linearLayoutManager);
            nearbyBusRv.setAdapter(busLineAdapter);
            List<QueryStandLinesRes.BusLineArraysBean.BusLineListBean> busLineList = viewData.get(position).getBusLineList();
            mList.clear();
            for (int i = 0; i < busLineList.size(); i++) {
                QueryStandLinesRes.BusLineArraysBean.BusLineListBean busLineListBean = busLineList.get(i);
                String json = new Gson().toJson(busLineListBean);
                QueryBusLineNearbyRes.BusLineArrayListBean.BusLineListBean listBean = new Gson().fromJson(json, QueryBusLineNearbyRes.BusLineArrayListBean.BusLineListBean.class);
                mList.add(listBean);
            }
            String json = new Gson().toJson(mList);
            busLineAdapter.setViewData(mList);
            busLineAdapter.notifyDataSetChanged();

        }

    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
    }

    public void setViewData(List<QueryStandLinesRes.BusLineArraysBean> viewData) {
        this.viewData = viewData;
    }


    public static class BusNearbyLineHolder extends BaseHolder {
        private BusNearbyLineItemBinding binding;

        public static BusNearbyLineHolder creat(ViewGroup parent) {

            BusNearbyLineItemBinding binding = BusNearbyLineItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);

            return new BusNearbyLineHolder(binding);
        }


        public BusNearbyLineHolder(BusNearbyLineItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
